package com.yjt.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yjt.admin.system.entity.Menu;
import com.yjt.admin.system.form.MenuForm;
import com.yjt.admin.system.param.MenuParam;
import com.yjt.admin.system.service.MenuService;
import com.yjt.admin.system.vo.MenuVO;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-08
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/menu")
@Api(tags = "菜单管理")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping("/add")
    @ApiOperation(value = "增加菜单信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.MENU_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增菜单信息")
    public CommonResult addMenu(@RequestBody MenuForm menuForm) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuForm, menu);
        menuService.save(menu);
        return CommonResult.success("菜单新增成功");
    }


    @DeleteMapping("/delete")
    @ApiOperation(value = "删除菜单信息", notes = "返回JSONObject",hidden = true)
    @ApiImplicitParam(name = "menuId", value = "菜单id")
    @OperLog(operModul = ModulEnum.MENU_MODUL, operType = OperTypeEnum.DEL, operDesc = "删除菜单信息")
    public CommonResult delMenu(String menuId) {
        menuService.removeById(menuId);
        return CommonResult.success("菜单删除成功");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改菜单信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.MENU_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改菜单信息")
    public CommonResult updateMenu(@RequestBody MenuForm menuForm) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuForm, menu);
        menuService.updateById(menu);
        return CommonResult.success("菜单修改成功");
    }

    @GetMapping("/{menuId}")
    @ApiOperation(value = "查询菜单信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "menuId", value = "菜单id")
    public CommonResult<MenuVO> getOneMenu(@PathVariable String menuId) {
        MenuVO menuVO = new MenuVO();
        Menu menu = menuService.getById(menuId);
        BeanUtils.copyProperties(menu, menuVO);
        return CommonResult.success(menuVO, "菜单信息查询成功");
    }

    @PostMapping("/list-page")
    @ApiOperation(value = "查询分页菜单信息", notes = "返回JSONObject",hidden = true)
    public CommonResult<IPage<MenuVO>> getPageMenu(@RequestBody MenuParam menuParam) {
        return CommonResult.success(menuService.getPageMenu(menuParam), "分页菜单信息查询成功");
    }

}

